import Vue from 'vue'
import vuex from 'vuex'
import {Network, request} from '../utils/axios'

Vue.use(vuex)
//插件处理刷新问题
import savedata from 'vue-savedata'
const store = new vuex.Store({
  state: {
    userInfo: {
      name: '名字'
    },
    token: 'ASASD54564',
  },
  getters: {
    userInfo(state) {
      return state.userInfo
    },
    token(state) {
      return state.token + '_token'
    }
  },
  //处理同步更改state
  mutations: {
    setUserInfo(state, val) {
      state.userInfo = val;
    },
    setToken(state, val) {
      state.token = val;
    }
  },
  //处理异步更改state
  actions: {
    requestUserInfo(context) {
      return new Promise((resolve, reject) => {
        request.get('/getTangPoetry', null).then( data => {
          context.commit('setUserInfo',data)
          // console.log('打印')
          // console.log(context.getters.userInfo);
          resolve(data)
        });
      })
    },
    requestTwo(context) {
      this.dispatch('requestUserInfo').then( data => {
        //执行基于上一个异步操作的操作
      })
    }
  },
  plugins: [savedata()]
})

export default store